<?php

namespace App\Console\Commands\WorkOrder;

use App\Enums\WorkOrder\OrderEnum;
use App\Http\Form\KfCashRegisterV2\KfCashRegisterV2FormAttribute;
use App\Models\WorkOrder\WorkOrderApplyContentModel;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use Illuminate\Console\Command;

class InitBusinessLicenseDate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workOrder:InitBusinessLicenseDate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '兼容工单2.0营业执照注册日期-结束日期';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        WorkOrderApplyMainModel::query()
            ->with(['contentRel'])
            ->where('problemPid', OrderEnum::PROBLEM_PID_5)
//            ->where('problemChildId', OrderEnum::PROBLEM_PID_5_CHILDREN_105)
            ->chunkById(
                100, function ($workOrderApplyMainModel) {
                foreach ($workOrderApplyMainModel as $item) {

                    if ($item && $item->contentRel) {
                        $content = json_decode($item->contentRel->content ?? "", true);
                        if ($content) {
                            $mergeData = [];
                            foreach ($content as $k => $v) {
                                if ($k == 'businessLicenseValidityPeriod' &&
                                    (isset($v['startTime']) && $v['startTime'] && isset($v['endTime']) && $v['endTime'])) { //夸父收银进件2.0 营业执照有效期
                                    $mergeData['businessLicenseRegistrationDate']   = $v['startTime']; //注册时间
                                    $mergeData['businessLicenseEndDate']          = $v['endTime']; //过期时间 长期-2099-12-31
                                    $mergeData['businessLicenseValidityPeriodType'] =
                                        $v['endTime'] == '2099-12-31' ?
                                            KfCashRegisterV2FormAttribute::BUSINESS_LICENSE_TYPE_LONG :
                                            KfCashRegisterV2FormAttribute::BUSINESS_LICENSE_TYPE;
                                    break;
                                }
                                if ($k == 'effectiveTime' &&
                                    (isset($v['startTime']) && $v['startTime'] && isset($v['endTime']) && $v['endTime'])) { //许可证上的准确有效期
                                    $mergeData['effectiveTimeStartDate'] = $v['startTime'];                                 //注册时间
                                    $mergeData['effectiveTimeEndDate']   = $v['endTime'];                                   //过期时间
                                    break;
                                }
                            }

                            if ($mergeData) {
                                $content = array_merge($content, $mergeData);
                                WorkOrderApplyContentModel::query()
                                    ->where('id', $item->contentRel->id)
                                    ->update(['content' => json_encode($content)]);
                            }
                        }
                    }
                }
            }
            );
    }
}
